/**********************
George Swartzendruber
w387u669
Program 1
***********************/

#include <stdio.h>
#include <math.h>

int testNum=3, testDiv=3, lastNum=2;
int i=2,j=2,count=0, interval=0, totalPrimes=1;

int main (void){
 
  //get number of primes desired
  printf("How many prime numbers do you want (at least 10)?\n");
  scanf("%d", &count);
  if(count<10)
    printf("You must enter an integer greater than 10!\n");
  else{
  
    //get desired intervals
    printf("At what interval do you want display the prime numbers? (0, 1, 10, 100, or 1000)\n");
    scanf("%d", &interval);
    if(interval != 0 && interval != 1 && interval != 10 && interval != 100 && interval != 1000)
      printf("You must enter either 0, 1, 10, 100, or 1000!\n");
    else{
      
      printf("Index\t\tNumber\t\tComposites since last prime\t%%Prime\n");//creates headings for the output table
      printf("1\t\t2\t\t1\t\t\t\t50.00%%\n");
      
      for(i=2; i<count+1; i++){//starts the count at 2 since "2" is the first prime and is already given
	testDiv = testNum-1; //allows me to always enter the loop
	while((testNum-1) % testDiv == 0){//increments testNum until prime is actually found
	  for(testDiv=2; (double)testDiv <= sqrt((double)testNum) && testNum % testDiv; testDiv++)//checks if divisible until testDiv = sqrt(testNum) or it finds that is is divisible
	    ;
	  testNum+=1;
	}
	totalPrimes++;//keeps track of how many primes I have found
	if(i==2 && interval==1){//takes care of error concerning first run through
	  printf("%d\t\t%d\t\t%d\t\t\t\t%.2f%%\n", i,testNum-1,testNum-lastNum-2,(double)totalPrimes/testNum*100);
	  j=1;
	}
	else{
	  if(j==interval){//check to see if the user wants this number printed
	    printf("%d\t\t%d\t\t%d\t\t\t\t%.2f%%\n", i,testNum-1,testNum-lastNum-2,(double)totalPrimes/testNum*100);
	    j=1;
	  }
	  else{
	    j++;
	  }
	}
	if(i==count && (interval==0 || count%interval!=0))//checks to see if last number was printed and if not it prints it
	printf("%d\t\t%d\t\t%d\t\t\t\t%.2f%%\n", i,testNum-1,testNum-lastNum-2,(double)totalPrimes/testNum*100);
	lastNum = testNum-1;//keeps track of the last prime number for purpose of finding composites
      }
    }
  }
	
  return 0;  
}
